package org.jfk.core.web.taglib;

import java.io.IOException;

import javax.servlet.jsp.JspException;

import org.jfk.core.utils.I18nUtil;

import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.ehcache.CacheKit;

/**
 * 根据ID取指定字段值的标签
 * @author jxf
 * 2015年4月1日
 */
public class KvTag extends AbstractTag {
	
	private String key ;
	private String table;
	private String column;



	public int doEndTag() throws JspException {
		String returnStr="";

		try {
			if (key != null && !key.equals(""))
			{
				returnStr = CacheKit.get("KvTag", table+key+column);
				if (StrKit.isBlank(returnStr)){
					Record r = Db.findById(table, Integer.valueOf(key).intValue());
					if (r != null){
						returnStr =  r.getStr(column);
					}else{
						returnStr = I18nUtil.getValueByKey("tag.kv.unknown") + "-" + key;
					}
					CacheKit.put("KvTag", table+key+column, returnStr);
				}
			}			
			pageContext.getOut().print(returnStr);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return EVAL_PAGE;
	}
	


	public String getKey() {
		return key;
	}


	public void setKey(String key) {
		this.key = key;
	}

	
	public String getTable() {
		return table;
	}



	public void setTable(String table) {
		this.table = table;
	}



	public String getColumn() {
		return column;
	}



	public void setColumn(String column) {
		this.column = column;
	}
	
}
	